package com.jiaai.filter;

import com.jiaai.util.HttpClientUtil;
import com.jiaai.util.ResponseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * class_name:IpAddressLogInterceptor
 * describe:------------------>记录ip地址
 * , 并且设置请求头, 允许跨域:
 *  response.setHeader("Access-Control-Allow-Origin","*");
    response.setHeader("Access-Control-Allow-Headers","Authorization,token,Content-Type");
 * create_user:张昉平
 * create_date:2018/1/20
 * create_time:14:50
 */
@Component
public class IpAddressLogInterceptor extends HandlerInterceptorAdapter {

    private static final Logger xlog = LoggerFactory.getLogger(IpAddressLogInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        ResponseUtils.setHeaderCors(response);
        xlog.error(("ip地址:").concat(HttpClientUtil.getIpAddress(request)).concat(", 访问路径:").concat(request.getRequestURI()));
        return true;
    }
}
